package com.cqut.controller;


import com.cqut.VO.result.ResultVO;
import com.cqut.VO.topic.TopicDetailsVO;
import com.cqut.VO.topic.TopicListVO;
import com.cqut.entity.Topic;
import com.cqut.service.TopicService;
import com.cqut.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@RestController
@RequestMapping("//topic")
public class TopicController {

    @Autowired
    private TopicService topicService;

    @PutMapping("/launch")
    public ResultVO<Object> launchTopic(@RequestBody Topic topic) {
        if (topicService.launchTopic(topic)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getTopicList/{courseId}/{userType}")
    public ResultVO<List<TopicListVO>> getTopicList(@PathVariable("courseId") Integer courseId,
                                                    @PathVariable("userType") Integer userType) {
        List<TopicListVO> topicList = topicService.getTopicList(courseId, userType);
        if (topicList != null) {
            return ResultVOUtil.success(topicList);
        }
        return ResultVOUtil.fail();
    }

    @DeleteMapping("/deleteTopic/{topicId}")
    public ResultVO<Object> deleteTopic(@PathVariable("topicId") Integer topicId) {
        if (topicService.deleteTopic(topicId)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getTopicDetails/{topicId}")
    public ResultVO<TopicDetailsVO> getTopicDetailsByTopicId(@PathVariable("topicId") Integer topicId) {
        TopicDetailsVO topicDetails = topicService.getTopicDetails(topicId);
        if (topicDetails != null) {
            return ResultVOUtil.success(topicDetails);
        }
        return ResultVOUtil.fail();
    }
}

